#include "stm32f10x.h"
uint16_t TIM_PRESCALER = 72;

void InputCapture_Init(void)
{
    // enable tim4
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    // enable rcc-tim4
    // select internal clock input mode
    TIM_InternalClockConfig(TIM4);
    // configure time-base unit
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitParam;
    TIM_TimeBaseInitParam.TIM_ClockDivision = TIM_CKD_DIV1; // test git good
    TIM_TimeBaseInitParam.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitParam.TIM_Period = 65536 - 1;
    TIM_TimeBaseInitParam.TIM_Prescaler = TIM_PRESCALER - 1; // hz = 72 * 1000 * 1000 / 72 = 1000 * 1000hz
    TIM_TimeBaseInitParam.TIM_RepetitionCounter = 1;

    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitParam);


    // init gpio for tix
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_InitParam;
    GPIO_InitParam.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitParam.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitParam.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitParam);

    // init input capture
    TIM_ICInitTypeDef TIM_ICInitParam;
    TIM_ICInitParam.TIM_Channel = TIM_Channel_1;
    TIM_ICInitParam.TIM_ICFilter = 0xF;
    TIM_ICInitParam.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitParam.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitParam.TIM_ICSelection = TIM_ICSelection_DirectTI;

    TIM_ICInit(TIM4, &TIM_ICInitParam);
    TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1);
    // configure master/slave mode
    TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

    TIM_Cmd(TIM4, ENABLE);
}

void InputCapture_Init2(void)
{
    
    // enable rcc-tim4
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    // select input clock mode
    TIM_InternalClockConfig(TIM4);
    // configure time-base unit
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitParam;
    TIM_TimeBaseInitParam.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitParam.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitParam.TIM_Period = 65536 - 1;
    TIM_TimeBaseInitParam.TIM_Prescaler = TIM_PRESCALER - 1; 
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitParam);
    // configure slave mode
    TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1);
    TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

    // enable rcc-gpiob6
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_InitParam;
    GPIO_InitParam.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitParam.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitParam.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitParam);

    // configure pwmi
    TIM_ICInitTypeDef TIM_ICInitParam;
    TIM_ICInitParam.TIM_Channel = TIM_Channel_1;
    TIM_ICInitParam.TIM_ICFilter = 0xF;
    TIM_ICInitParam.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitParam.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitParam.TIM_ICSelection = TIM_ICSelection_DirectTI;
    // TIM_ICInit(TIM4, &TIM_ICInitParam);

    // TIM_ICInitParam.TIM_Channel = TIM_Channel_2;
    // TIM_ICInitParam.TIM_ICFilter = 0xF;
    // TIM_ICInitParam.TIM_ICPolarity = TIM_ICPolarity_Falling;
    // TIM_ICInitParam.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    // TIM_ICInitParam.TIM_ICSelection = TIM_ICSelection_IndirectTI;
    // TIM_ICInit(TIM4, &TIM_ICInitParam);

    TIM_PWMIConfig(TIM4, &TIM_ICInitParam);
    // enable tim4
    TIM_Cmd(TIM4, ENABLE);
}

uint16_t Get_Freq(void)
{
    // icc/ tim4freq
    return  (72 * 1000 * 1000/ TIM_PRESCALER) / TIM_GetCapture1(TIM4);
}
uint16_t Get_Duty(void)
{
    return TIM_GetCapture2(TIM4) * 100 / TIM_GetCapture1(TIM4);
}

